package com.zy.system.service.impl;

import java.util.Date;
import java.util.List;

import com.zy.common.utils.DateUtils;
import com.zy.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zy.system.mapper.SysUserOnlineMapper;
import com.zy.system.domain.SysUserOnline;
import com.zy.system.service.ISysUserOnlineService;
import com.zy.common.support.Convert;

/**
 * 在线用户记录 服务层实现
 *
 * @author zy
 * @date 2018-12-19
 */
@Service
public class SysUserOnlineServiceImpl implements ISysUserOnlineService
{
    @Autowired
    private SysUserOnlineMapper userOnlineMapper;


    /**
     * 查询在线用户记录列表
     *
     * @param sysUserOnline 在线用户记录信息
     * @return 在线用户记录集合
     */
    @Override
    public List<SysUserOnline> selectSysUserOnlineList(SysUserOnline sysUserOnline)
    {
        return userOnlineMapper.selectSysUserOnlineList(sysUserOnline);
    }

    /**
     * 新增在线用户记录
     *
     * @param sysUserOnline 在线用户记录信息
     * @return 结果
     */
    @Override
    public int insertSysUserOnline(SysUserOnline sysUserOnline)
    {
        return userOnlineMapper.insertSysUserOnline(sysUserOnline);
    }

    /**
     * 修改在线用户记录
     *
     * @param sysUserOnline 在线用户记录信息
     * @return 结果
     */
    @Override
    public int updateSysUserOnline(SysUserOnline sysUserOnline)
    {
        return userOnlineMapper.updateSysUserOnline(sysUserOnline);
    }

    /**
     * 删除在线用户记录对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteSysUserOnlineByIds(String ids)
    {
        return userOnlineMapper.deleteSysUserOnlineByIds(Convert.toStrArray(ids));
    }

    @Override
    public SysUserOnline selectOnlineById(String sessionId)
    {
        return userOnlineMapper.selectOnlineById(sessionId);
    }

    @Override
    public void deleteOnlineById(String sessionId)
    {
        SysUserOnline userOnline = selectOnlineById(sessionId);
        if (StringUtils.isNotNull(userOnline))
        {
            userOnlineMapper.deleteSysUserOnlineById(sessionId);
        }
    }

    /**
     * 查询会话集合
     *
     * @param expiredDate 有效期
     * @return 会话集合
     */
    @Override
    public List<SysUserOnline> selectOnlineByExpired(Date expiredDate)
    {
        String lastAccessTime = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, expiredDate);

        return userOnlineMapper.selectOnlineByExpired(lastAccessTime);
    }

    /**
     * 通过会话序号删除信息
     *
     * @param sessions 会话ID集合
     * @return 在线用户信息
     */
    @Override
    public void batchDeleteOnline(List<String> sessions)
    {
        for (String sessionId : sessions)
        {
            SysUserOnline userOnline = selectOnlineById(sessionId);
            if (StringUtils.isNotNull(userOnline))
            {
                userOnlineMapper.deleteSysUserOnlineById(sessionId);
            }
        }
    }

}
